package com.glowdayz.glowmee.bc;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.common.Util;
import com.glowdayz.glowmee.cmd.SecondCategoryCMD;
import com.glowdayz.glowmee.dao.FirstCategoryDAO;
import com.glowdayz.glowmee.dao.SecondCategoryDAO;

public class SecondCategoryBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	public SecondCategoryBC() {
        super();
    }
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		request.setCharacterEncoding("utf-8");
		String page_str = request.getParameter("page");
		String pageCnt_str = request.getParameter("pageCnt");
		int idFirst = Integer.parseInt(request.getParameter("idFirst"));
		
		int page = 0;
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		int pageCnt = 0;
		if (pageCnt_str == null) {
			pageCnt = 10;
		} else {
			pageCnt = Integer.parseInt(pageCnt_str);
		}
		
		String param_str = request.getParameter("param");
		String updateIdFirstCategory_str = request.getParameter("updateIdFirstCategory" + param_str);
		String updateSecondCategoryText = request.getParameter("updateSecondCategoryText" + param_str);
		String updateKeyWord = request.getParameter("updateKeyWord" + param_str);
		String updateSortKey_str = request.getParameter("updateSortKey" + param_str);
		String updateIsDisplay_str = request.getParameter("updateIsDisplay" + param_str);
		
		if (param_str != null && updateIdFirstCategory_str != null && updateSecondCategoryText != null && 
				updateKeyWord != null && updateSortKey_str != null && updateIsDisplay_str != null) {
			int param = Integer.parseInt(param_str);
			int updateIdFirstCategory = Integer.parseInt(updateIdFirstCategory_str);
			updateSecondCategoryText = Util.getInstance().incoding(updateSecondCategoryText);
			updateKeyWord = Util.getInstance().incoding(updateKeyWord);
			int updateSortKey = Integer.parseInt(updateSortKey_str);
			int updateIsDisplay = Integer.parseInt(updateIsDisplay_str);
			try {
				SecondCategoryCMD cmd = new SecondCategoryCMD(param, updateIdFirstCategory, updateSecondCategoryText, updateKeyWord, 
						updateSortKey, updateIsDisplay);
				SecondCategoryDAO.getInstance().updateSecondCategory(cmd);
				request.setAttribute("alert", "<script type='text/javascript'>alert('수정이 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String insertSecondCategoryText = request.getParameter("insertSecondCategoryText");
		String insertKeyWord = request.getParameter("insertKeyWord");
		String insertSortKey_str = request.getParameter("insertSortKey");
		String insertIsDisplay_str = request.getParameter("insertIsDisplay");
		
		if (insertSecondCategoryText != null && insertKeyWord != null && insertSortKey_str != null && insertIsDisplay_str != null) {
			int insertSortKey = Integer.parseInt(insertSortKey_str);
			int insertIsDisplay = Integer.parseInt(insertIsDisplay_str);
			insertSecondCategoryText = Util.getInstance().incoding(insertSecondCategoryText);
			insertKeyWord = Util.getInstance().incoding(insertKeyWord);
			try {
				SecondCategoryCMD cmd = new SecondCategoryCMD(idFirst, insertSecondCategoryText, insertKeyWord, insertSortKey, insertIsDisplay);
				SecondCategoryDAO.getInstance().insertSecondCategory(cmd);
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("pageCnt", pageCnt);
			request.setAttribute("first", FirstCategoryDAO.getInstance().getFirstCategoryList(1, -1, "isDisplay=1"));
			request.setAttribute("firstInfo", FirstCategoryDAO.getInstance().getFirstCategory(idFirst));
			request.setAttribute("second", SecondCategoryDAO.getInstance().getSecondCategoryList(page, pageCnt, "idFirstCategory=" + idFirst));
			request.setAttribute("display", DBHandler.getInstance().getCount(
					"secondcategory", "idSecondCategory", "isDisplay='1' AND idFirstCategory=" + idFirst));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount(
					"secondcategory", "idSecondCategory", "isDisplay='0' AND idFirstCategory=" + idFirst));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/category/secondCategory.jsp");
		dpr.forward(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		String[] param_str = request.getParameterValues("param");
		String[] updateIdFirstCategory_str = request.getParameterValues("updateIdFirstCategory");
		String[] updateSecondCategoryText = request.getParameterValues("updateSecondCategoryText");
		String[] updateKeyWord = request.getParameterValues("updateKeyWord");
		String[] updateSortKey_str = request.getParameterValues("updateSortKey");
		String[] updateIsDisplay_str = new String[param_str.length];
		String[] update_list = request.getParameterValues("update_list");
		
		for (int i=0; i<param_str.length; i++) {
			String isDisplay_temp = request.getParameter("updateIsDisplay" + param_str[i]);
			updateIsDisplay_str[i] = isDisplay_temp;
		}
		
		for (int i=0; i<param_str.length; i++) {
			for (int j=0; j<update_list.length; j++) {
				if (param_str[i].equals(update_list[j])) {
					if (param_str[i] != null && updateIdFirstCategory_str[i] != null && updateSecondCategoryText[i] != null && 
							updateKeyWord[i] != null && updateSortKey_str[i] != null && updateIsDisplay_str[i] != null) {
						int param = Integer.parseInt(param_str[i]);
						int updateIdFirstCategory = Integer.parseInt(updateIdFirstCategory_str[i]);
						int updateSortKey = Integer.parseInt(updateSortKey_str[i]);
						int updateIsDisplay = Integer.parseInt(updateIsDisplay_str[i]);
						try {
							SecondCategoryCMD cmd = new SecondCategoryCMD(param, updateIdFirstCategory, updateSecondCategoryText[i], updateKeyWord[i], 
									updateSortKey, updateIsDisplay);
							SecondCategoryDAO.getInstance().updateSecondCategory(cmd);
						} catch (Exception e) {
							DBHandler.getInstance().disconnect();
							e.printStackTrace();
						}
					}
				}
			}
		}
		
		String page_str = request.getParameter("page");
		String pageCnt_str = request.getParameter("pageCnt");
		int idFirst = Integer.parseInt(request.getParameter("idFirst"));
		
		int page = 0;
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		int pageCnt = 0;
		if (pageCnt_str == null) {
			pageCnt = 10;
		} else {
			pageCnt = Integer.parseInt(pageCnt_str);
		}
		
		try {
			request.setAttribute("alert", "<script type='text/javascript'>alert('선택수정이 완료되었습니다.')</script>");
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("pageCnt", pageCnt);
			request.setAttribute("first", FirstCategoryDAO.getInstance().getFirstCategoryList(1, -1, "isDisplay=1"));
			request.setAttribute("firstInfo", FirstCategoryDAO.getInstance().getFirstCategory(idFirst));
			request.setAttribute("second", SecondCategoryDAO.getInstance().getSecondCategoryList(page, pageCnt, "idFirstCategory=" + idFirst));
			request.setAttribute("display", DBHandler.getInstance().getCount(
					"secondcategory", "idSecondCategory", "isDisplay='1' AND idFirstCategory=" + idFirst));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount(
					"secondcategory", "idSecondCategory", "isDisplay='0' AND idFirstCategory=" + idFirst));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/category/secondCategory.jsp");
		dpr.forward(request, response);
	}
	

}
